<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>goods detail</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
          integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
            integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
            crossorigin="anonymous"></script>
    <!-- jquery-validator -->
    <script src="https://cdn.bootcss.com/jquery-validate/1.19.1/jquery.validate.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery-validate/1.19.1/localization/messages_zh.js"></script>
    <!--    layer-->
    <script src="https://cdn.bootcss.com/layer/1.8.5/layer.min.js"></script>
    <!-- md5.js -->
    <!--    <script src="https://github.com/emn178/js-md5/blob/master/build/md5.min.js"></script>-->
    <script type="text/javascript" src="/js/md5.min.js"></script>
    <!-- common.js -->
    <script type="text/javascript" src="/js/common.js"></script>
</head>
<body>
<div class="panel panel-default">
    <div class="panel-heading">
        <span>details<br/></span>
        <span id="userTip"> 您还没有登录，请登陆后再操作<br/></span>
        <span>没有收货地址的提示。。。</span>
    </div>
    <div class="panel-body">
        <table class="table" id="goodslist">
            <tr>
                <td>商品名称</td>
                <td colspan="3" id="goodsName"></td>
            </tr>
            <tr>
                <td>商品图片</td>
                <td colspan="3"><img id="goodsImg" width="200" height="200"/></td>
            </tr>
            <tr>
                <td>秒杀开始时间</td>
                <td id="startTime"></td>
                <td>
                    <input type="hidden" id="remainSeconds"/>
                    <span id="spikeTip"></span>
                </td>
                <td>
                    <button class="btn btn-primary btn-block" type="button" id="buyButton" onclick="doSpike()">立即秒杀
                    </button>
                    <input type="hidden" name="goodsId" id="goodsId"/>
                </td>
            </tr>
            <tr>
                <td>商品原价</td>
                <td colspan="3" id="goodsPrice"></td>
            </tr>
            <tr>
                <td>秒杀价</td>
                <td colspan="3" id="spikePrice"></td>
            </tr>
            <tr>
                <td>库存数量</td>
                <td colspan="3" id="stockCount"></td>
            </tr>
        </table>
    </div>
    <div class="panel-footer">try your luck</div>
</div>
</body>
<script>
    $(function () {
        getDetail();
    });

    function render(detail) {
        var remainSeconds = detail.remainSeconds;
        var goods = detail.goodsVo;
        var user = detail.spikeUser;
        if (user != null) {
            $("#userTip").hide();
        }
        $("#goodsName").text(goods.goodsName);
        $("#goodsImg").attr("src", goods.goodsImg);
        $("#startTime").text(new Date(goods.startDate).format("yyyy-MM-dd hh:mm:ss"));
        $("#remainSeconds").val(remainSeconds);
        $("#goodsId").val(goods.id);//[JavaScript]不支持全局变量，但有时我们必须用全局变量，我们就可以把值先存在隐藏域里，它的值就不会丢失了
        $("#goodsPrice").text(goods.goodsPrice);
        $("#spikePrice").text(goods.spikePrice);
        $("#stockCount").text(goods.stockCount);
        countDown();
    }

    function getDetail() {
        var goodsId = g_getQueryString("goodsId");
        $.get({
            url: "/goods/detail/" + goodsId,
            success: function (data) {
                if (0 === data.code) {
                    render(data.data);//goodsDetailVo
                } else {
                    layer.msg(data.msg);
                }
            },
            error: function () {
                layer.msg("client request error");
            }
        });
    }

    function countDown() {
        var remainSeconds = $("#remainSeconds").val();
        var timeout;
        if (parseInt(remainSeconds) > 0) {//秒杀还没开始，倒计时
            $("#buyButton").attr("disabled", true);
            $("#spikeTip").html("秒杀倒计时：" + remainSeconds + "秒");
            timeout = setTimeout(function () {
                $("#countDown").text(remainSeconds - 1);
                $("#remainSeconds").val(remainSeconds - 1);
                countDown();
            }, 1000);
        } else if (remainSeconds == 0) {//秒杀进行中
            $("#buyButton").attr("disabled", false);
            if (timeout) {
                clearTimeout(timeout);
            }
            $("#spikeTip").html("秒杀进行中");
        } else {//秒杀已经结束
            $("#buyButton").attr("disabled", true);
            $("#spikeTip").html("秒杀已经结束");
        }
    }

    function doSpike() {
        $.post({
            url: "spike/do_spike",
            data: {goodsId: $("#goodsId").val()},
            success: function (data) {//order
                if (data.code === 0) {
                    layer.msg("正在处理中");
                    getSpikeResult($("#goodsId").val);
                } else {
                    layer.msg(data.msg);
                }
            },
            error: function () {
                layer.msg("client request error");
            }

        });
    }

    function getSpikeResult(goodsId) {
        g_showLoading();
        $.ajax({
            url: "/spike/result",
            type: "GET",
            data: {
                goodsId: $("#goodsId").val(),
            },
            success: function (data) {
                if (data.code == 0) {
                    var result = data.data;
                    if (result < 0) {
                        layer.msg("对不起，秒杀失败");
                    } else if (result == 0) {//继续轮询
                        setTimeout(function () {
                            getSpikeResult(goodsId);
                        }, 50);
                    } else {
                        window.location.href = "/order_detail.htm?orderId=" + result;
                    }
                } else {
                    layer.msg(data.msg);
                }
            },
            error: function () {
                layer.msg("客户端请求有误");
            }
        });
    }


</script>


</html>